package com.strawcom.charmring;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.litepal.LitePalApplication;
import org.litepal.crud.DataSupport;

import android.annotation.SuppressLint;
import android.graphics.Typeface;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;

import com.android.volley.Request.Method;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.strawcom.charmring.bean.DailySummary;
import com.strawcom.charmring.bean.http.BaseResponse;
import com.strawcom.charmring.bean.http.CidRequest;
import com.strawcom.charmring.util.HttpUtil;
import com.strawcom.charmring.util.HttpUtil.ResponeListener;
import com.strawcom.charmring.util.LogUtil;
import com.strawcom.charmring.util.PreferenceUtil;

/**
 * 
 * @ClassName: App
 * @Description:全局Application
 * @author liyongqiang
 * @date 2015-3-30 上午12:03:14
 * 
 */
public class App extends LitePalApplication {

	private static App instance;
	public RequestQueue mQueue;
	/** 已经被幼圆替换 */
	public Typeface tfYouYuan;
	/** 数字字体 */
	public Typeface tfGeometr212;

	private DailySummary dailySummary;
	private SimpleDateFormat sdfRecordDate;
	public String today = "";

	@SuppressLint("SimpleDateFormat")
	@Override
	public void onCreate() {
		super.onCreate();

		instance = this;
		
		CrashHandler.getInstance().setCustomCrashHanler(this);
		
		mQueue = Volley.newRequestQueue(this);
//		InputStream keyStore =getResources().openRawResource(R.raw.charmring);
//		mQueue = Volley.newRequestQueue(this,
//                         new ExtHttpClientStack(new SslHttpClient(keyStore,"123456", 8443)));
		
		tfYouYuan = Typeface.createFromAsset(getAssets(),
				C.app.fonts_YouYuan);
		tfGeometr212 = Typeface.createFromAsset(getAssets(),
				C.app.fonts_Geometr212);
		
		sdfRecordDate = new SimpleDateFormat(C.exercise.record_date_pattern);

		today = sdfRecordDate.format(new Date());
		
		// init global exercise info
		App.i().getDailySummary();
		
	}

	public static synchronized App i() {
		return instance;
	}

	@Override
	public void onTerminate() {
		super.onTerminate();
		// mQueue.stop();
	}

	public DailySummary getDailySummary() {
		return getDailySummary(false);
	}

	public synchronized DailySummary getDailySummary(boolean mandatory) {
		today = sdfRecordDate.format(new Date());
		if (dailySummary == null || mandatory) {
			List<DailySummary> dailySummaryList = DataSupport.where("date = ?",
					today).find(DailySummary.class);
			if (dailySummaryList != null && dailySummaryList.size() > 0) {
				dailySummary = dailySummaryList.get(0);
			} else {
				dailySummary = DailySummary.newIntance(instance);
			}
		} else if (!today.equals(dailySummary.getDate())) {
			dailySummary = DailySummary.newIntance(instance);
		}
		return dailySummary;
	}

	public SimpleDateFormat getSdfRecordDate() {
		return sdfRecordDate;
	}


	public void showShare(String shareContent, File shareImage) {
		ShareSDK.initSDK(this);
		OnekeyShare oks = new OnekeyShare();
		// 关闭sso授权
		oks.disableSSOWhenAuthorize();
		// 分享时Notification的图标和文字 2.5.9以后的版本不调用此方法
		// oks.setNotification(R.drawable.ic_launcher,
		// getString(R.string.app_name));
		// title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
		oks.setTitle(getString(R.string.share));
		// titleUrl是标题的网络链接，仅在人人网和QQ空间使用
		oks.setTitleUrl(C.app.download_url);
		// text是分享文本，所有平台都需要这个字段
		oks.setText(shareContent);
		// imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
		oks.setImagePath(shareImage.getAbsolutePath());// 确保SDcard下面存在此张图片
		// url仅在微信（包括好友和朋友圈）中使用
		oks.setUrl(C.app.download_url);
		// comment是我对这条分享的评论，仅在人人网和QQ空间使用
		//oks.setComment("我是测试评论文本");
		// site是分享此内容的网站名称，仅在QQ空间使用
		oks.setSite(getString(R.string.app_name));
		// siteUrl是分享此内容的网站地址，仅在QQ空间使用
		// 启动分享GUI
		oks.show(this);
	}
	
	public void uploadCid(final String cid) {
		String phone=PreferenceUtil.readString(this, C.sp.phone,"");
		String password=PreferenceUtil.readString(this, C.sp.password,"");
		if(!"".equals(phone)&&!"".equals(password)){
			CidRequest request = new CidRequest();
			request.msgid = "POST_DEVICE_TOKEN";
			request.devicetoken = cid;
			HttpUtil.requestJson(Method.POST, request, new ResponeListener() {
				@Override
				public void onResponse(BaseResponse response, String resultStr) {
					if(response.retcode==0){
						PreferenceUtil.write(App.this, C.sp.isBindCidSueecss, true);
					}
					LogUtil.d("POST_DEVICE_TOKEN onResponse");
				}

				@Override
				public void onErrorResponse(Exception error) {
					LogUtil.e("POST_DEVICE_TOKEN onErrorResponse");
				}
			});
		}
	}
}
